package com.zf.zfed.audit.set

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import com.zf.zfed.audit.R
import com.zf.zfed.audit.VideoPlayerActivity
import com.zf.zfed.audit.databinding.FragmentHelpCenterBinding
import com.zf.zfed.audit.tools.extension.singleClick

/**
 * 帮助中心Fragment
 * 包含视频播放和PDF文档查看功能
 */
class HelpCenterFragment : Fragment() {
    private lateinit var binding: FragmentHelpCenterBinding
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentHelpCenterBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        setupClickListeners()
    }

    /**
     * 设置点击事件
     */
    private fun setupClickListeners() {
        // 返回按钮
//        binding.btnBack.singleClick {
//            try {
//                // 关闭Activity
//                activity?.finish()
//            } catch (e: Exception) {
//                e.printStackTrace()
//                Toast.makeText(context, "返回失败: ${e.message}", Toast.LENGTH_SHORT).show()
//            }
//        }

        // 播放视频按钮
        binding.btnPlayVideo.singleClick {
            playTutorialVideo()
        }

        // 打开PDF按钮
        binding.btnOpenPdf.singleClick {
            openUserManualPdf()
        }
    }

    /**
     * 播放教程视频
     */
    private fun playTutorialVideo() {
        try {
            // 启动全屏视频播放Activity
            val intent = Intent(requireContext(), VideoPlayerActivity::class.java)
            intent.putExtra("video_path", "file:///android_asset/操作视频.mp4")
            startActivity(intent)
        } catch (e: Exception) {
            Toast.makeText(
                requireContext(),
                "播放视频失败: ${e.message}",
                Toast.LENGTH_SHORT
            ).show()
        }
    }

    /**
     * 打开用户手册PDF
     */
    private fun openUserManualPdf() {
        try {
            // 使用我们自己的PDF查看器打开
            val intent = Intent(requireContext(), com.zf.zfed.audit.PdfViewerActivity::class.java)
            intent.putExtra(com.zf.zfed.audit.PdfViewerActivity.EXTRA_PDF_NAME, "用户手册.pdf")
            startActivity(intent)
        } catch (e: Exception) {
            Toast.makeText(
                requireContext(),
                "打开PDF失败: ${e.message}",
                Toast.LENGTH_SHORT
            ).show()
        }
    }

    companion object {
        /**
         * 创建新的HelpCenterFragment实例
         */
        fun newInstance(): HelpCenterFragment {
            return HelpCenterFragment()
        }
    }
}